<?php

// 循环统计 | 五码
// GET ctrl.pk10.loop
// http://sg.io/api?ctrl.pk10.loop

namespace app\api\resource\ctrl\pk10;
use think\Db;

class Loop
{
    // 方法
    public $_method = ['GET'];
    // 前置
    public $_pre    = [];
    // public $_pre    = ['member'];
    // 描述
    public $_description = '循环统计';
    // 参数
    public $_param  = [];

    public function run(&$request)
    {
        $sTime = strtotime( date('Y-m-d 00:00:00') );
        $eTime = strtotime( date('Y-m-d 23:59:59') );
        $list  = Db::name('plan')->where('v1_loc','>',0)
                                 ->where('create_time','>',$sTime)
                                 ->where('create_time','<',$eTime)
                                 ->field('no,v1_loc,v2_loc,v3_loc,v4_loc,v5_loc,create_time')
                                 ->order('no DESC')
                                 ->select();
        // echo count($list);p($list);die;

        $item = [
            'v1' => [
                'no'   => 0,
                'total'=> 0,
                'count'=> 0,
                'date' => 0,
            ],
            'v2' => [
                'no'   => 0,
                'total'=> 0,
                'count'=> 0,
                'date' => 0,
            ],
            'v3' => [
                'no'   => 0,
                'total'=> 0,
                'count'=> 0,
                'date' => 0,
            ],
            'v4' => [
                'no'   => 0,
                'total'=> 0,
                'count'=> 0,
                'date' => 0,
            ],
            'v5' => [
                'no'   => 0,
                'total'=> 0,
                'count'=> 0,
                'date' => 0,
            ],
        ];

        if ( empty($list) ) {
            unset(
                    $item['v1']['count'],
                    $item['v2']['count'],
                    $item['v3']['count'],
                    $item['v4']['count'],
                    $item['v5']['count']
                  );
            return [200,$item];
        }

        $no    = 0;
        $total = 0;
        $count = 0;
        foreach ($list as $key => $val) {
            for($i=1;$i<=5;$i++) {
                $n = ($val['v' . $i . '_loc'] > 5) ? 1 : 0;
                if ($n > 0) {
                    $item['v' . $i]['count']--;
                    if ($item['v' . $i]['count'] < $item['v' . $i]['total']) {
                        $item['v' . $i]['total'] = $item['v' . $i]['count'];
                        $item['v' . $i]['no']    = $val['no'];
                        $item['v' . $i]['date']  = date('H:i:s',$val['create_time']);
                        $item['v' . $i]['create_time']  = $val['create_time'];
                    }
                } else {
                    $item['v' . $i]['count']++;
                }
            }
        }
        // p($item);die;

        unset(
                $item['v1']['count'],
                $item['v2']['count'],
                $item['v3']['count'],
                $item['v4']['count'],
                $item['v5']['count']
              );
        // p($item);die;

        return [200,$item];
    }
}
